//
//  SelectShreaView.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/5/14.
//  Copyright © 2016年 game. All rights reserved.
//

#import "SelectShreaView.h"
#import <ShareSDK/ShareSDK.h>
#import "WXApi.h"
#import "WeiboSDK.h"
#import <TencentOpenAPI/QQApiInterface.h>
#import "MBLabelWithFontAdapter.h"
#import "MBButtonWithFontAdapter.h"

/*   <!-- 应用规范字体大小 -->*/
#define  text_size_big_1    21
#define  text_size_big_2    20
#define  text_size_big_3    19
#define  text_size_middle_1 18
#define  text_size_middle_2 17
#define  text_size_middle_3 15
#define  text_size_little_1 14
#define  text_size_little_2 13
#define  text_size_little_3 12
#define  text_size_little_4 10
#define  text_size_other    16

#define  AppFont(c_font) [UIFont systemFontOfSize:c_font]
#define ApplicationframeValue [[UIScreen mainScreen]bounds].size
CG_INLINE void AlertLog (NSString *titleStr,NSString *message,NSString *button1,NSString *button2)
{
    if(!titleStr)
        titleStr = @"";

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle: titleStr
                                                        message: message
                                                       delegate: nil
                                              cancelButtonTitle: button1
                                              otherButtonTitles: button2,
                              nil];
    [alertView show];

}

@interface SelectShreaView ()
{
    
    CGFloat _hhhh;
}
@property (nonatomic,strong) NSArray *imageArr;
@property (nonatomic,strong) NSArray *titleArr;
@property (nonatomic,strong) UIView *backView;

@end

@implementation SelectShreaView
-(NSArray *)imageArr
{
    
    if (!_imageArr) {
        _imageArr = [NSArray arrayWithObjects:@"qq_share~iphone",@"zone-1~iphone",@"weibo~iphone",@"weixin~iphone",@"friend~iphone",@"shouchang~iphone",@"jubao~iphone",nil];
    }
    return _imageArr;
}

-(NSArray *)titleArr
{
    
    if (!_titleArr) {
        _titleArr = [NSArray arrayWithObjects:@"QQ",@"空间",@"微博",@"微信",@"朋友圈",@"收藏"
                     ,@"举报",nil];
    }
    return _titleArr;
}


-(id)initWithFrame:(CGRect)frame
{
    
    _hhhh = frame.size.height;
    
    self = [super initWithFrame:frame];
    if (self) {
        self.overlayView = [[UIControl alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self.overlayView.backgroundColor = TouMingBlack2;
        [self.overlayView addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
        
        UIView *viewS = [[UIView alloc] initWithFrame:CGRectMake(0, _hhhh, ApplicationframeValue.width,_hhhh)];
        viewS.backgroundColor =[UIColor whiteColor];
        [self addSubview:viewS];
        
        
        for (int i = 0; i < [self.imageArr count]; i++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.tag = i +1;
            btn.adjustsImageWhenDisabled = NO;
            btn.adjustsImageWhenHighlighted = NO;
            
            int row = i%4;
            int colum = i/4;
            CGFloat xd = 25;
            CGFloat yd = 50;
            CGFloat ww = (SCREEN_WIDTH - xd*3-15*2)/4;
            CGFloat yy = (_hhhh-ww*2-15*2-yd)/2;
            
            btn.frame = CGRectMake(15+(xd+ww)*row,yy+(yd+ww)*colum, ww, ww);
            [btn setBackgroundImage:[UIImage imageNamed:self.imageArr[i]] forState:UIControlStateNormal];
            
            MBLabelWithFontAdapter *label = [[MBLabelWithFontAdapter alloc] initWithFrame:CGRectMake(btn.x, Height(btn)+10, ww, 15)];
            
            label.textAlignment = NSTextAlignmentCenter;
            label.text = self.titleArr[i];
            label.font = AppFont(text_size_little_2);
            label.adjustsFontSizeToFitWidth = YES;
            [btn addTarget:self action:@selector(onBtn:) forControlEvents:UIControlEventTouchUpInside];
            [viewS addSubview:label];
            [viewS addSubview:btn];
        }
            self.backView= viewS;
        
        
    }
    return self;
}

-(void)cancelClick{
    
    [self fadeOut];
}

-(void)setModel:(SelectthreadModel *)model{
    _model = model;
    self.shareUrl = _model.share_url;
    self.title  = _model.title;
    self.imageUrl = _model.share_img;
    self.message = _model.share_message;
}

-(void)onBtn:(UIButton *)sender{
    
    switch (sender.tag) {
            
        case 1:{//qq好友
            if ([QQApiInterface isQQInstalled]) {
                
                [self ShareWhithSSDKPlatformType:SSDKPlatformSubTypeQQFriend];
                
                
            }else{
                AlertLog(@"", @"没有安装QQ客户端，无法进行QQ分享", @"确定", nil);
            }
        }
            break;
            
            
        case 2:{//空间
            
            if ([QQApiInterface isQQInstalled]) {

                 [self ShareWhithSSDKPlatformType:SSDKPlatformSubTypeQZone];
            }
            else{
                AlertLog(@"", @"没有安装QQ客户端，无法进行空间分享", @"确定", nil);
            }
        }
            break;
            
        case 3:{//微博
            if ([WeiboSDK isCanShareInWeiboAPP]) {
                
                [self ShareWhithSSDKPlatformType:SSDKPlatformTypeSinaWeibo];
                
                
            }
            else{
                AlertLog(@"", @"没有安装微博客户端，无法进行微博分享", @"确定", nil);
                
            }
        }
            break;
        case 4:{//微信
            if ([WXApi isWXAppInstalled]) {
                
                [self ShareWhithSSDKPlatformType:SSDKPlatformSubTypeWechatSession];
            }
            else{
                AlertLog(@"", @"没有安装微信客户端，无法进行微信分享", @"确定", nil);
                
            }
        }
            break;
        case 5:{//朋友圈
            if ([WXApi isWXAppInstalled]) {
                
                [self ShareWhithSSDKPlatformType:SSDKPlatformSubTypeWechatTimeline];
                
            }
            else{
                AlertLog(@"", @"没有安装微信客户端，无法进行朋友圈分享", @"确定", nil);
                
            }
        }
            break;
            
        case 6:{//收藏
            [self dismiss];
            [self Collect];
            

        }
            break;
            
        case 7:{//举报
            [self dismiss];
            self.fatherCtrl.reportView.alpha = 1;
            [self.fatherCtrl.view bringSubviewToFront:self.fatherCtrl.reportView];
            

        }
            break;
            
    }
}
#pragma 收藏Action
-(void)Collect{
    
     [[Manager sharedManager]RequestWhithURL:GetFormhash AndParameter:nil AndCompletionType:^(id responseObject, DIEError *error) {
        if ([responseObject[@"status"] isEqualToString:@"SUCCESS"]) {
            [self CollectWhirtFormHash:responseObject[@"data"][@"formhash"]];
        }else{
            [Manager GetHudWhithText:@"收藏失败" AndFatherView:self.fatherCtrl.view];
        
        }
    }];


}

-(void)CollectWhirtFormHash:(NSString *)formHash{

    NSString *url = [NSString stringWithFormat:SelectCollect,(long)self.model.tid.integerValue,formHash];
    [[Manager sharedManager]RequestWhithURL:url AndParameter:nil AndCompletionType:^(id responseObject, DIEError *error) {
        
        [Manager GetHudWhithText:responseObject[@"message"] AndFatherView:self.fatherCtrl.view];
    }];


}


-(void)ShareWhithSSDKPlatformType:(NSInteger)SSDKPlatformType{
    
    
    NSString *utf8String = self.shareUrl?self.shareUrl:nil;
    
    
    NSArray* imageArray =@[self.imageUrl?self.imageUrl:nil];
    if (imageArray) {
        
        NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
        [shareParams SSDKEnableUseClientShare];
        [shareParams SSDKSetupShareParamsByText:self.message?self.message:nil
                                         images:imageArray
                                            url:[NSURL URLWithString:utf8String]
                                          title:self.title?self.title:nil
                                           type:SSDKContentTypeAuto];
        
        
        [ShareSDK share:SSDKPlatformType
             parameters:shareParams
         onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
             
             if (state == SSDKResponseStateSuccess)
             {
                 [self fadeOut];
             }
             else if (state == SSDKResponseStateFail)
             {
                 AlertLog(@"", @"分享失败", @"确定", nil);
             }
             
             
         }];
        
    }
    
    
}


-(void)onCancleBtn{
    
    [self dismiss];
    
}


- (void)show
{
    UIWindow *keywindow = [[UIApplication sharedApplication] keyWindow];
    [keywindow addSubview:self.overlayView];
    [keywindow addSubview:self];
    
    [self fadeIn];
}

- (void)dismiss
{
    [self fadeOut];
}

//弹入层
- (void)fadeIn
{
    self.alpha = 1;
    [UIView animateWithDuration:0.3 animations:^{
        self.backView.y = 0;
    } completion:nil];
    
}

//弹出层
- (void)fadeOut
{
    
    [UIView animateWithDuration:0.3 animations:^{
        self.backView.y = _hhhh;
    } completion:^(BOOL finished) {
        
        self.alpha = 0;
        [self.overlayView removeFromSuperview];
        [self removeFromSuperview];
    }];
    
}



@end
